﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Bidder_BidAccCom : System.Web.UI.Page
{
    /// <summary>
    /// 
    /// </summary>
    
    string BiddingSwitching, count1;
    string _strTable = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        string sql;
        Int32 PJID;
        Int64 AccountId_TBR;
        PJID = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["ProjectId"]));
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                Db db = new Db();
                System.Data.IDataReader dr;
                AccountId_TBR = SysFun.ToLong(_Account.AccountId);                
                //判断是否能投标之投标人没有投标权限
                sql = "select min(BiddingSwitching) as BiddingSwitching,count(*) as cun From ProjectCompanyAttribution where accountid=" + AccountId_TBR.ToString() + " and projectid=" + PJID.ToString();
                dr = db.DataReader(sql);
                while (dr.Read())
                {
                    count1 = dr["cun"].ToString();
                    BiddingSwitching = dr["BiddingSwitching"].ToString();
                }
                dr.Close();
                db.Close();
                if (count1 == "0" || count1 == "")
                {

                    Response.Write("<script>window.alert('您的企业暂时还没有被列为投标人，暂时不能投标，如有疑问请于招标中心联系！');</script>");
                    Response.Write("<script>window.location=\"Nothing.aspx?Projectid=" + PJID.ToString() + "\"</script>");

                }
                else
                {
                    showGridView();
                }
            }
        }
    }
    public void showGridView()
    {
        string strConditions;
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = _strTable;
        dbPage.Key = "Company.AccountId";
        dbPage.Where = strConditions;                        //
        dbPage.SelectField = "distinct Company.AccountId,Company.CompanyName,Company.UserCode," + SysFun.IsSQL(Request.QueryString["ProjectId"]) + " as ProjectId,a.Num";
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 20;//Request.QueryString["PS"]);
        dbPage.OrderBy = "Company.CompanyName,Company.UserCode";
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        db.Close();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键


    }
    public string fctConditions()
    {
        string _strWhere = "";
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (_Account != null)
        {
            int _intProjectId = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["ProjectId"]));

            if (_Account.Sort == "0")
            {
                _strTable = " company left join (select distinct AccountId, 1 as Num from  project_bid  where   project_bid.AccountId=" + _Account.AccountId.ToString() + " and project_bid.ProjectId=" + _intProjectId.ToString() + ") a on Company.AccountId=a.AccountId INNER JOIN bid_goods_tbr on Company.AccountId=bid_goods_tbr.accountid_sc";
                _strWhere += " Company.AccountId=" + _Account.AccountId.ToString() + " and bid_goods_tbr.Accountid_TB=" + _Account.AccountId + " and";
            }
            else
            {
                _strTable = " company inner join (select  Goods.AccountId_SC as userid_sc, max(project_bid.GoodsId)  as Num from goods left join project_bid on goods.GoodsId=project_bid.GoodsId and   project_bid.AccountId=" + _Account.AccountId.ToString() + " and project_bid.ProjectId=" + _intProjectId.ToString() + "  group by  Goods.AccountId_SC) a on Company.AccountId=a.userid_sc INNER JOIN bid_goods_tbr on Company.AccountId=bid_goods_tbr.accountid_sc";
                _strWhere += " Company.accountid in (select accountid_sc from bid_goods_tbr where accountid_tb=" + _Account.AccountId.ToString() + ")   and bid_goods_tbr.Accountid_TB=" + _Account.AccountId + " and";
            }

            string _strCompanyName = txtCompanyName.Text;
            if (_strCompanyName != "")
            {
                _strWhere += " Company.CompanyName like '%" + _strCompanyName + "%' And";
            }
            string _strUserCode = txtUserCode.Text;
            if (_strUserCode != "")
            {
                _strWhere += " Company.UserCode like '%" + _strUserCode + "%' And";
            }
            if (_strWhere != "")
            {
                _strWhere = "  " + _strWhere.Substring(0, _strWhere.Length - 3);
            }

        }
        return _strWhere;

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int tb = SysFun.ToInt(DataBinder.Eval(e.Row.DataItem, "Num"));
            if (tb != 0)
            {
                e.Row.BackColor = System.Drawing.Color.Gold;
            }
        }
        if ((BiddingSwitching == "0") || (count1 == "0"))
        {
            e.Row.Cells[2].Visible = false;
        }

    }
}

